<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="#" align="center" type="index" width="50" fixed="left" />
      <el-table-column label="数据元名称" prop="itemName" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.itemName`" :rules="formRules.itemName" class="mb-0px!">
            <el-input v-model="row.itemName" placeholder="请输入数据元名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="数据元英文" prop="itemEnglish" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.itemEnglish`" :rules="formRules.itemEnglish" class="mb-0px!">
            <el-input v-model="row.itemEnglish" placeholder="请输入数据元英文" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="数据元类型" prop="itemType" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.itemType`" :rules="formRules.itemType" class="mb-0px!">
            <el-select v-model="row.itemType" placeholder="请选择数据元类型">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.DATA_ELEMENT_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="描述" prop="itemDesc" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.itemDesc`" :rules="formRules.itemDesc" class="mb-0px!">
            <el-input v-model="row.itemDesc" placeholder="请输入描述" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加数据项</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { propTypes } from '@/utils/propTypes'
import { DataApi } from '@/api/dr/data'

const emit = defineEmits(['change'])
const props = defineProps({
  dataId: propTypes.string.def(''), // 共享数据主键（主表的关联字段）
})
const formLoading = ref(false) // 表单的加载中
const formData = ref<any>([])
const formRules = reactive<any>({
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.dataId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await DataApi.getDataItemListByDataId(val)
      emitChange()
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row:any = {
    id: undefined,
    dataId: undefined,
    itemName: undefined,
    itemEnglish: undefined,
    itemType: undefined,
    itemDesc: undefined,
  }
  row.dataId = props.dataId
  formData.value.push(row)
  emitChange()
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
  emitChange()
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}
const emitChange = () => {
  emit('change', formData.value)
}

defineExpose({ validate, getData })
</script>